/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs;

import com.cxhd.pacs.actor.ActorManager;
import com.cxhd.pacs.controller.AddAskCtrl;
import com.cxhd.pacs.controller.ConfigCtrl;
import com.cxhd.pacs.controller.IndexCtrl;
import com.cxhd.pacs.controller.MainCtrl;
import com.cxhd.pacs.controller.RecviceCtrl;
import com.cxhd.pacs.controller.user.AskCtrl;
import com.cxhd.pacs.controller.user.HostCtrl;
import com.cxhd.pacs.controller.user.InHospitalCtrl;
import com.cxhd.pacs.controller.user.OtherCtrl;
import com.cxhd.pacs.controller.user.PacsCtrl;
import com.cxhd.pacs.controller.user.PatDataCtrl;
import com.cxhd.pacs.controller.user.PatientCtrl;
import com.cxhd.pacs.handler.MyBaseHandler;
import com.cxhd.pacs.interceptor.HostInfoInterceptor;
import com.cxhd.pacs.model.Asklist;
import com.cxhd.pacs.model.CacheData;
import com.cxhd.pacs.model.FileDetail;
import com.cxhd.pacs.model.FileSystem;
import com.cxhd.pacs.model.HostDetail;
import com.cxhd.pacs.model.HostInfo;
import com.cxhd.pacs.model.Inhospital;
import com.cxhd.pacs.model.LisDetail;
import com.cxhd.pacs.model.Template;
import com.cxhd.pacs.model.TemplateDetail;
import com.cxhd.pacs.websocket.StartServer;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;

/**
 *
 * @author Administrator
 */
public class PACSConfig extends JFinalConfig {

    public DataConfig dataconfig;
    private ActorManager actorManager;
    StartServer sss;

    @Override
    public void configConstant(Constants c) {

        PropKit.use("a_little_config.txt");				// 加载少量必要配置，随后可用PropKit.get(...)获取值
        c.setDevMode(PropKit.getBoolean("devMode", true));
        c.setViewType(ViewType.JSP);
    }

    @Override
    public void configRoute(Routes routes) {
        routes.add("/", IndexCtrl.class);
        routes.add("/admin/config", ConfigCtrl.class);
        routes.add("/patient", HostCtrl.class,"/patient");
        routes.add("/inhos", InHospitalCtrl.class);
        routes.add("/patInfo", PatientCtrl.class);
        routes.add("/main", MainCtrl.class);
        routes.add("/main/view/add", AddAskCtrl.class);
        routes.add("/main/view/recvice", RecviceCtrl.class);
        routes.add("/main/view/ask", AskCtrl.class);
        routes.add("/pat", PatDataCtrl.class);
        routes.add("/pacs", PacsCtrl.class);
        routes.add("/other", OtherCtrl.class);
        
    }

    public static DruidPlugin createDruidPlugin() {
        return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
    }

    @Override
    public void configPlugin(Plugins plgns) {
        DruidPlugin altasCenter = createDruidPlugin();
        plgns.add(altasCenter);
        // 配置ActiveRecord插件 
        ActiveRecordPlugin arp_altasCenter = new ActiveRecordPlugin(altasCenter);
        arp_altasCenter.setDialect(new MysqlDialect());
        arp_altasCenter.setShowSql(false);
        plgns.add(arp_altasCenter);
        arp_altasCenter.addMapping("hostinfo", "pk", HostInfo.class);
        arp_altasCenter.addMapping("inhospital", "pk", Inhospital.class);
        arp_altasCenter.addMapping("host_detail", "pk", HostDetail.class);
        arp_altasCenter.addMapping("template", "pk", Template.class);
        arp_altasCenter.addMapping("template_detail", "pk", TemplateDetail.class);
        arp_altasCenter.addMapping("file_detail", "pk", FileDetail.class);
        arp_altasCenter.addMapping("filesystem", "pk", FileSystem.class);
        arp_altasCenter.addMapping("lis_detail", "pk", LisDetail.class);
        arp_altasCenter.addMapping("cachedata", "pk", CacheData.class);
        arp_altasCenter.addMapping("asklist", "pk", Asklist.class);
        System.out.println("初始化数据库");
    }

    @Override
    public void configInterceptor(Interceptors i) {
        i.add(new HostInfoInterceptor());
    }

    @Override
    public void configHandler(Handlers me) {
        me.add(new ContextPathHandler("base"));
        me.add(new MyBaseHandler());
    }

    @Override
    public void afterJFinalStart() {

        super.afterJFinalStart(); //To change body of generated methods, choose Tools | Templates.
        dataconfig = DataConfig.getInstance();

        actorManager = ActorManager.getIntance();

        actorManager.initActor();
        dataconfig.initConfig();
        System.out.println(DataConfig.getInstance().checkPlugin("plugin_lis_data"));
//        try {
//            sss = new StartServer(13134,1);
//
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        sss.run(); 
//                    } catch (Exception ex) {
//                    }
//                }
//            }).start();
//        } catch (Exception e) {
//            e.printStackTrace();
//            if (sss != null) {
//
//                sss.closeServer();
//            }
//        }

    }

    @Override
    public void beforeJFinalStop() {
        super.beforeJFinalStop(); //To change body of generated methods, choose Tools | Templates.
        actorManager.destory();
        if (sss != null) {

            sss.closeServer();
        }

    }

}
